package cn.itcast.crm.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerRepository customerRepository;

	public List<Customer> findNoAssociationCustomers() {
		return customerRepository.findByFixedAreaIdIsNull();
	}

	public List<Customer> findHasAssociationFixedAreaCustomers(String fixedAreaId) {

		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	public void associationCustomersToFixedArea(String customerIdStr, String fixedAreaId) {

		// 解除定区与客户之间的关联
		customerRepository.clearFixedAreaId(fixedAreaId);

		// 取出所有传入的客户id,并将该客户与定区关联
		if (StringUtils.isBlank(customerIdStr)) {
			return;
		}
		String[] ids = customerIdStr.split(",");
		for (int i = 0; i < ids.length; i++) {
			Integer id = Integer.parseInt(ids[i]);
			customerRepository.updateFixedAreaId(fixedAreaId, id);
		}

	}

}
